package com.lgmshare.k3.ui.list;

import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.lgmshare.component.helper.MediaHelper;
import com.lgmshare.component.logger.Logger;
import com.lgmshare.component.utils.FileUtils;
import com.lgmshare.component.utils.UIUtils;
import com.lgmshare.component.widget.recyclerview.DragItemTouchCallback;
import com.lgmshare.component.widget.recyclerview.GridItemDecoration;
import com.lgmshare.component.widget.recyclerview.HeaderFooterValue;
import com.lgmshare.component.widget.recyclerview.RecyclerViewAdapter;
import com.lgmshare.component.widget.recyclerview.RecyclerViewHeaderFooterAdapter;
import com.lgmshare.k3.R;
import com.lgmshare.k3.ui.base.BaseActivity;
import com.lgmshare.k3.ui.list.viewholder.MessageHolder;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;

public class MessageListActivity extends BaseActivity implements RecyclerViewAdapter.OnItemClickListener,
        RecyclerViewAdapter.OnItemChildClickListener,
        RecyclerViewHeaderFooterAdapter.OnHeaderClickListener,
        RecyclerViewHeaderFooterAdapter.OnFooterClickListener {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private ShootCircleAdapter mShootCircleAdapter;

    @Override
    protected int onBindLayoutResId() {
        return R.layout.base_ui_pull_list_activity;
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initLoad() {
        mShootCircleAdapter = new ShootCircleAdapter(getActivity());
        mShootCircleAdapter.setOnItemClickListener(this);
        mShootCircleAdapter.setOnItemChildClickListener(this);
        mShootCircleAdapter.setOnFooterClickListener(this);
        mShootCircleAdapter.setOnHeaderClickListener(this);

        mShootCircleAdapter.addHeaderViewHolder(MessageHolder.class, new HeaderFooterValue());
        mShootCircleAdapter.addFooterViewHolder(MessageHolder.class, new HeaderFooterValue());

        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        recyclerView.addItemDecoration(new GridItemDecoration(getActivity(), 2, 8, true));
        recyclerView.setAdapter(mShootCircleAdapter);
        DragItemTouchCallback dragItemTouchCallback = new DragItemTouchCallback();
        dragItemTouchCallback.setDragEnable(true);
        dragItemTouchCallback.setSwipeEnable(true);
        dragItemTouchCallback.setOnItemTouchCallbackListener(new DragItemTouchCallback.OnItemTouchCallbackListener() {

            @Override
            public void onSwiped(int adapterPosition) {

            }

            @Override
            public boolean onMove(int srcPosition, int targetPosition) {
                //在原方法中可以根据viewType判断
                int fromPosition = srcPosition - mShootCircleAdapter.getHeaderCount();
                int toPosition = targetPosition - mShootCircleAdapter.getHeaderCount();
                if (fromPosition < 0 || toPosition < 0) {
                    return false;
                }

                List<String> list = mShootCircleAdapter.getList();
                if (fromPosition >= list.size() || toPosition >= list.size()) {
                    return false;
                }

                if (fromPosition < toPosition) {
                    for (int i = fromPosition; i < toPosition; i++) {
                        Collections.swap(list, i, i + 1);
                    }
                } else {
                    for (int i = fromPosition; i > toPosition; i--) {
                        Collections.swap(list, i, i - 1);
                    }
                }
                mShootCircleAdapter.notifyItemMoved(srcPosition, targetPosition);
                return true;

            }
        });
        final ItemTouchHelper itemTouchHelp = new ItemTouchHelper(dragItemTouchCallback);
        itemTouchHelp.attachToRecyclerView(recyclerView);

        loadData();
    }

    protected void loadData() {
        mShootCircleAdapter.setList(imageList());
        mShootCircleAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(View view, int position) {
        showToast("index:" + position);
    }

    @Override
    public void onHeaderClick(int position) {
        showToast("index:" + position);

    }

    @Override
    public void onFooterClick(int position) {
        mShootCircleAdapter.removeFooter(0);
    }

    @Override
    public void onItemChildClick(View view, int position) {
        String item = mShootCircleAdapter.getItem(position);
        MediaHelper.downloadFileToMediaStore(getActivity(), item, null, MediaHelper.TYPE_IMAGE, new MediaHelper.OnFileDownloadListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onFinish() {

            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onProgress(long totalSize, long currentSize, long offset) {

            }

            @Override
            public void onSuccess(Uri uri) {
                UIUtils.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (uri != null) {
                            Logger.d(TAG, "图片保存成功");
                        } else {
                            Logger.d(TAG, "图片保存失败");
                        }
                    }
                });
            }

            @Override
            public void onFailure(String errorMsg) {
                Logger.d(TAG, "图片保存失败：" + errorMsg);
            }
        });
       /* UIUtils.runOnAsyncThread(new Runnable() {
            @Override
            public void run() {
                Bitmap imageBitmap = getImageBitmap(item);
                Uri uri = MediaHelper.saveImageToMediaStore(getActivity(), imageBitmap);
                UIUtils.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (uri != null) {
                            Logger.d(TAG, "图片保存成功");
                        } else {
                            Logger.d(TAG, "图片保存失败");
                        }
                    }
                });
            }
        });*/
    }

    private Bitmap getImageBitmap(String url) {
        try {
            RequestOptions ro = new RequestOptions();
            ro.diskCacheStrategy(DiskCacheStrategy.ALL);
            ro.decode(Bitmap.class);
            Bitmap bitmap = Glide.with(getActivity())
                    .asBitmap()
                    .load(url)
                    .submit(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                    .get();
            return bitmap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<String> imageList() {

        List<String> mUrls = new ArrayList<String>();
        mUrls.add("http://e.hiphotos.baidu.com/image/pic/item/a1ec08fa513d2697e542494057fbb2fb4316d81e.jpg");
        mUrls.add("http://c.hiphotos.baidu.com/image/pic/item/30adcbef76094b36de8a2fe5a1cc7cd98d109d99.jpg");
        mUrls.add("http://h.hiphotos.baidu.com/image/pic/item/7c1ed21b0ef41bd5f2c2a9e953da81cb39db3d1d.jpg");
        mUrls.add("http://g.hiphotos.baidu.com/image/pic/item/55e736d12f2eb938d5277fd5d0628535e5dd6f4a.jpg");
        mUrls.add("http://e.hiphotos.baidu.com/image/pic/item/4e4a20a4462309f7e41f5cfe760e0cf3d6cad6ee.jpg");
        mUrls.add("http://b.hiphotos.baidu.com/image/pic/item/9d82d158ccbf6c81b94575cfb93eb13533fa40a2.jpg");
        mUrls.add("http://e.hiphotos.baidu.com/image/pic/item/4bed2e738bd4b31c1badd5a685d6277f9e2ff81e.jpg");
        mUrls.add("http://g.hiphotos.baidu.com/image/pic/item/0d338744ebf81a4c87a3add4d52a6059252da61e.jpg");
        mUrls.add("http://a.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee5080c8142ff5e0fe99257e19.jpg");
        mUrls.add("http://f.hiphotos.baidu.com/image/pic/item/4034970a304e251f503521f5a586c9177e3e53f9.jpg");
        mUrls.add("http://b.hiphotos.baidu.com/image/pic/item/279759ee3d6d55fbb3586c0168224f4a20a4dd7e.jpg");
        mUrls.add("http://img2.xkhouse.com/bbs/hfhouse/data/attachment/forum/corebbs/2009-11/2009113011534566298.jpg");
        mUrls.add("http://a.hiphotos.baidu.com/image/pic/item/e824b899a9014c087eb617650e7b02087af4f464.jpg");
        mUrls.add("http://c.hiphotos.baidu.com/image/pic/item/9c16fdfaaf51f3de1e296fa390eef01f3b29795a.jpg");
        mUrls.add("http://d.hiphotos.baidu.com/image/pic/item/b58f8c5494eef01f119945cbe2fe9925bc317d2a.jpg");
        mUrls.add("http://h.hiphotos.baidu.com/image/pic/item/902397dda144ad340668b847d4a20cf430ad851e.jpg");
        mUrls.add("http://b.hiphotos.baidu.com/image/pic/item/359b033b5bb5c9ea5c0e3c23d139b6003bf3b374.jpg");
        mUrls.add("http://a.hiphotos.baidu.com/image/pic/item/8d5494eef01f3a292d2472199d25bc315d607c7c.jpg");
        mUrls.add("http://b.hiphotos.baidu.com/image/pic/item/e824b899a9014c08878b2c4c0e7b02087af4f4a3.jpg");
        mUrls.add("http://g.hiphotos.baidu.com/image/pic/item/6d81800a19d8bc3e770bd00d868ba61ea9d345f2.jpg");
        return mUrls;
    }
}
